﻿var lantu = {};
/**
 * svg http://documentup.com/wout/SVG.js#radius-1
 * 
 */
lantu.Application = Class.extend(
{
    NAME : "lantu.Application", 

    /**
     * @constructor
     * 
     * @param {String} canvasId the id of the DOM element to use as paint container
     */
    init : function(canvasId)
    { 	  
		var _this = this;
		//this.paper    = new Raphael(canvasId); 
		this.svg = SVG(canvasId);
		this.paperdom=$("#"+canvasId);
		/*this.paper.canvas.style.position="relative ";
		this.paper.canvas.style.left="0px";
		this.paper.canvas.style.top="0px";*/
		this.paperdom.css({"cursor":"crosshair"});
		this.paperdom.css({"-webkit-tap-highlight-color": "rgba(0,0,0,0)"});
		this.onDrawing=true;//绘图模块
		this.currCmd="select";//当前操作
		this.mouseDown  = false;//鼠标按下标识
		this.ox;
        this.oy;
		this.line;
		////鼠标点击后松开 或者触屏后手指离开
		this.paperdom.bind("mouseup touchend", function(event)
		{	 _this.mouseDown  = false;
			 //event = _this._getEvent(event);
			 _this.onMouseUp(event.clientX,event.clientY);
		});
		//鼠标左键按着，并移动或者触屏移动
        this.paperdom.bind("mousemove touchmove", function(event)
		{
			if(	 _this.mouseDown){
			_this.onMouseDrag(event.clientX,event.clientY);
			}
		});
		 //鼠标点击 或者触屏
        this.paperdom.bind("mousedown touchstart", function(event){
			if(!_this.onDrawing ) {
				console.log(" unselect tool");
				return true;
			}
            var pos = null;
            switch (event.which) {
            case 1: //触屏
				
            case 0: //左键
					_this.mouseDown = true;
					event.preventDefault();
                    event = _this._getEvent(event);
					_this.ox = event.clientX-_this.paperdom.offset().left;
                    _this.oy = event.clientY-_this.paperdom.offset().top;
					_this.onMouseDown(_this.mouseDownX,_this.mouseDownY);
				break;
			case 3: //右键
				break;
			case 2:
                //中间键
                break;
             default:
			}
                
		});
		this.paperdom.on("click",function(event){
			
		});
	}
	,
	onMouseUp:function(x,y){
		if(!this.onDrawing ) return;
		var _x=x-this.paperdom.offset().left;
		var _y=y-this.paperdom.offset().top;
		if(this.line!=undefined){
			this.line.plot(this.ox, this.oy, _x, _y);
			//this.line.select().resize()
			this.line.draggable();
			//this.line.rect.selectize(false);//反选
			this.line.selectize({deepSelect:true});
		     
		}
	},
	onMouseDrag:function(x,y){
		if(!this.onDrawing ) return;
		var _x=x-this.paperdom.offset().left;
		var _y=y-this.paperdom.offset().top;
		if(this.line!=undefined){
			this.line.plot(this.ox, this.oy, _x, _y);
		}
	},
	onMouseDown:function(x,y){
		if(!this.onDrawing ) return;
		//this.path = this.paper.path("M"+x+" "+y+"L100 100");
		//this.path.attr({x:x,y:y})
		var _x=x-this.paperdom.offset().left;
		var _y=y-this.paperdom.offset().top;
		
		
		this.line = this.svg.line(_x, _y, _x, _y).stroke({ width: 1 })
	}
	,
	onMouseMove:function(droppedDomNode){
		var _node=$(droppedDomNode);
        var type = _node.data("shape");
		var attr = _node.data("attr");
		
		
		switch(type)
		{
			case "rect":
				this.paper.rect(_node.x, _node.y, 50, 50, 10)
				break;
			case "fddraw":
			
				break;
		}
	},
	 _getEvent: function(event)
    {
      // check for iPad, Android touch events
      //
      if(typeof event.originalEvent !== "undefined"){  
          if(event.originalEvent.touches && event.originalEvent.touches.length) {
               return event.originalEvent.touches[0];
          } else if(event.originalEvent.changedTouches && event.originalEvent.changedTouches.length) {
               return event.originalEvent.changedTouches[0];
          }
      }
      return event;
    }
});

lantu.CommandStack = Class.extend({
    NAME : "lantu.CommandStack", 


    init: function( )
    {
       this.undostack = [];
       this.redostack = [];
       this.maxundo = 50;
	},
	 undo: function()
    {
       var command = this.undostack.pop();
       if(command)
       {
          this.redostack.push(command);
          command.undo();
       }
       
       return this;
    },
   
    redo: function()
    {
       var command = this.redostack.pop();
    
       if(command){
          this.undostack.push(command);
          command.redo();
       }
       
       return this;
    }
});